Skip to content

Compare APK versions without building token slices - #66

Merged
andrew merged 2 commits into
git-pkgs:mainfrom
charlie-morrison:apk-lockstep-compare
Nov 29, 2026
Merged

andrew merged 2 commits into
git-pkgs:mainfrom
charlie-morrison:apk-lockstep-compare

Conversation

@charlie-morrison

Copy link
Copy Markdown
Contributor

Refs #31, the APK item.

compareAPK built a []apkToken for each side (apk.go:41-42) before comparing. It now walks both versions in lockstep with a small apkScanner that yields one token at a time, using the same tokenizer rules in the same order. validAPKVersion and apkVersionIsPrerelease use the same scanner, so parseAPKVersion and apkTokenType are gone. apkVersionIsPrerelease lost its own invalid-token check, because the scanner ends at the first invalid token. Only a suffix before that token can decide, which is how the old loop behaved as well.

go test -bench 'BenchmarkCompareWithScheme/APK$' -benchmem -count 5, Go 1.26.7, linux/amd64 (a slow 2-core machine, so read the ratio, not the absolute numbers):

ns/op B/op allocs/op
main (70710c0) 966-1154 384 2
this branch 334-390 0 0

A version with more than six tokens outgrew the slice's initial capacity, so 1.2.3a_p1~abc-r2 against 1.2.3a_p1~abd-r1 allocated 4 times on main.

Coverage, following the ALPM/Conan/Gentoo rewrite:

  • apk_reference_test.go is a mechanically renamed copy of the old compareAPK / parseAPKVersion / apkTokenType.
  • TestCompareAPKMatchesReference covers every ordered pair of the shared parity corpus plus APK forms: each suffix with and without a number, commit hashes, revisions, letters, leading zeros, surrounding whitespace, overflowing numbers, and each way a version turns invalid part-way.
  • TestCompareAPKMatchesReferenceOnData covers every pair from apk-tools' apk_version.data and the conformance fixtures.
  • TestAPKClassifiersMatchReference checks validAPKVersion and apkVersionIsPrerelease against the reference on the same inputs.
  • FuzzCompareAPKMatchesReference ran 90 s here (275k execs), with no difference.
  • APK joins TestEcosystemComparatorsDoNotAllocate with three pairs: a suffix, hash plus revision, and an invalid suffix. All three fail on main, with 2, 4 and 2 allocations.
  • TestAPKScannerEndsAfterInvalidToken pins the one scanner property the comparator relies on but can't observe.

Mutations, one at a time, in a separate worktree: no TrimSpace, the end-of-input check off by one, validAPKVersion ignoring invalid tokens, the prerelease-suffix tie flipped, and the scanner continuing after an invalid token. All 5 turn a test red.

go test ./... ok, golangci-lint run 0 issues, gofmt clean. Exported APIs and the scheme ordering are unchanged. The branch is rebased on 2b66c01, where the only change since 70710c0 is the setup-go bump. Tests, lint and a bench rerun there (384-438 ns, 0 B, 0 allocs) give the same results.

compareAPK parsed each side into a []apkToken before comparing, which
cost two allocations per comparison (four once a version has more than
six tokens). It now walks both versions in lockstep with apkScanner,
which yields one token at a time; validAPKVersion and
apkVersionIsPrerelease use the same scanner.

The pre-change implementation is kept as apk_reference_test.go, and
the new comparator and both classifiers are checked against it on the
shared corpus plus APK forms, apk-tools' apk_version.data and the
conformance fixtures, with a fuzz target for the comparator. APK joins
TestEcosystemComparatorsDoNotAllocate.

Refs git-pkgs#31

Co-authored-by: Claude <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Explain Change →

Caution

Review failed

The pull request is closed.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@andrew
andrew merged commit 0f1fb9f into git-pkgs:main Nov 29, 2026
2 of 3 checks passed
@charlie-morrison
charlie-morrison deleted the apk-lockstep-compare branch November 29, 2026 18:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants